package com.soundplus.app.shp04.Common;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.soundplus.app.shp04.R;

import java.util.List;

public class CommonListAdapter extends BaseAdapter {
    Context context;
    List<CommonListItemInfo> itemList;
    LayoutInflater inflater;

    public CommonListAdapter(Context context, List<CommonListItemInfo> itemList) {
        this.context = context;
        this.itemList = itemList;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return itemList.size();
    }

    @Override
    public CommonListItemInfo getItem(int i) {
        return itemList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewGroup vg;

        if (view != null) {
            vg = (ViewGroup) view;
        } else {
            vg = (ViewGroup) inflater.inflate(R.layout.common_list_item, null);
        }

        final TextView name = vg.findViewById(R.id.tv_item_name);
        final TextView desc = vg.findViewById(R.id.tv_item_desc);

        CommonListItemInfo item = getItem(i);
        if (item.getName() != null) {
            name.setText(item.getName());
        } else {
            name.setText("Unknown");
        }

        if (item.getDesc() != null) {
            desc.setVisibility(View.VISIBLE);
            desc.setText(item.getDesc());
        } else {
            desc.setVisibility(View.INVISIBLE);
        }

        return vg;
    }
}
